package com.dsg.kf.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * 时间格式化工具类
 * 
 * @author Pacy.pan
 * @date 2016年11月17日
 *
 */
public class DateUtil {

	public static final String yyyy_MM_dd = "yyyy-MM-dd";

	public static final String yyyy_MM_ddHHmmss = "yyyy-MM-dd HH:mm:ss";

	public static final String yyMMddHHmmssSSS = "yyMMddhhmmssSSS";
	
	public static final String yyyy_MM_dd_HHmmssSSS = "yyyy-MM-dd HH:mm:ss.SSS";
	
	private static final String TQ_TIME_PRE="[0-9]{1,2}[:][0-9]{1,2}[:][0-9]{1,2}[.][0-9]{1,3}";
	
	public static final String MH_FLEX=":";
	public static final String DH_FLEX=".";

	public static final String yyyyMM="yyyyMM";
	
	/**
	 * 普通格式化：yyyy-MM-dd
	 * 
	 */
	public static String formatSimple() {
		return formatSimple(System.currentTimeMillis());
	}

	/**
	 * 普通格式化：yyyy-MM-dd
	 * 
	 */
	public static String formatSimple(Date date) {
		return formatSimple(date.getTime());
	}

	/**
	 * 普通格式化：yyyy-MM-dd
	 * 
	 */
	public static String formatSimple(long time) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_dd);
		return simpleDateFormat.format(time);
	}

	/**
	 * 带时分秒：yyyy-MM-dd HH:mm:ss
	 * 
	 */
	public static String format() {
		return format(System.currentTimeMillis());
	}

	/**
	 * 带时分秒：yyyy-MM-dd HH:mm:ss
	 * 
	 */
	public static String format(Date date) {
		return format(date.getTime());
	}

	/**
	 * 带时分秒：yyyy-MM-dd HH:mm:ss
	 * 
	 */
	public static String format(long time) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_ddHHmmss);
		return simpleDateFormat.format(time);
	}

	public static String format2(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_ddHHmmss);
		return simpleDateFormat.format(date);
	}
	
	public static String format3(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_dd_HHmmssSSS);
		return simpleDateFormat.format(date);
	}

	public static String formatSimple(String dateFormat,Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
		return simpleDateFormat.format(date);
	}
	
	/**
	 * 自定义
	 *
	 * @param exp
	 *            表达式
	 */
	public static String format(String exp) {
		return format(System.currentTimeMillis(), exp);
	}

	/**
	 * 自定义
	 *
	 * @param exp
	 *            表达式
	 */
	public static String format(Date date, String exp) {
		return format(date.getTime(), exp);
	}

	/**
	 * 自定义
	 *
	 * @param exp
	 *            表达式
	 */
	public static String format(long time, String exp) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(exp);
		return simpleDateFormat.format(time);
	}

	/**
	 * 日期
	 */
	public static int getDay() {
		return getDay(System.currentTimeMillis());
	}

	public static int getDay(Date date) {
		return getDay(date.getTime());
	}

	public static int getDay(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		return cal.get(Calendar.DATE);
	}

	/**
	 * 月份
	 */
	public static int getMonth() {
		return getMonth(System.currentTimeMillis());
	}

	public static int getMonth(Date date) {
		return getMonth(date.getTime());
	}

	public static int getMonth(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 年份
	 */
	public static int getYear() {
		return getYear(System.currentTimeMillis());
	}

	public static int getYear(Date date) {
		return getYear(date.getTime());
	}

	public static int getYear(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		return cal.get(Calendar.YEAR);
	}

	/**
	 * sql.Date
	 */
	public static java.sql.Date toSQLDate(Date date) {
		return date instanceof java.sql.Date ? (java.sql.Date) date : toSQLDate(date.getTime());
	}

	public static java.sql.Date toSQLDate(long time) {
		return new java.sql.Date(time);
	}

	public static java.sql.Date getSQLDate() {
		return toSQLDate(System.currentTimeMillis());
	}

	/**
	 * sql.Timestamp
	 */
	public static java.sql.Timestamp toTimestamp(Date date) {
		return date instanceof java.sql.Timestamp ? (java.sql.Timestamp) date : toTimestamp(date.getTime());
	}

	public static java.sql.Timestamp toTimestamp(long time) {
		return new java.sql.Timestamp(time);
	}

	public static java.sql.Timestamp getTimestamp() {
		return toTimestamp(System.currentTimeMillis());
	}

	public static String yesterdayStr(Date date, int num) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, num);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = new SimpleDateFormat(yyyy_MM_dd);
		String dateString = formatter.format(date);
		return dateString;
	}
	/**
	 * 根据时间字符串，将时分秒毫秒转化为long类型
	 * @param dateTime
	 * @return
	 */
	public static long getLongByTimeStr(String dateTime){
		 Pattern pattern = Pattern.compile(TQ_TIME_PRE);
	        Matcher matcher = pattern.matcher(dateTime);  
	        String dateStr = null;  
	        if(matcher.find()){  
	          dateStr = matcher.group(0);  
	        }  
	        String str =dateStr.toString(); 
	        str=str.replace(MH_FLEX, StringUtils.EMPTY);
	        str=str.replace(DH_FLEX, StringUtils.EMPTY);
	        long s = Long.parseLong(str);
	        return s;
	}
	/**
	 * 当期时间往后推hour小时
	 * 
	 * @param hour
	 * @return
	 */
	public static String getTimeByHour(int hour) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);
		return new SimpleDateFormat(yyyy_MM_ddHHmmss).format(calendar.getTime());
	}

	 /** 
     * 使用参数Format将字符串转为Date 
     */  
    public static Date strToDate(String strDate, String pattern)  
    {  
        try {
			return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(  
			        pattern).parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        return null;
    }  
	/**
	 * 获取当前时间之前或之后几分钟 minute
	 * 
	 * @param minute
	 * @return
	 */
	public static String getTimeByMinute(Date date, int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
	}

	public static String[] getTimeStrArr(Date date, int minute) {
		String[] arr = new String[6];
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		arr[0]=String.valueOf(now.get(Calendar.YEAR));
		arr[1]=String.valueOf((now.get(Calendar.MONTH) + 1) + "");
		arr[2]=String.valueOf(now.get(Calendar.DAY_OF_MONTH));
		arr[3]=String.valueOf(now.get(Calendar.HOUR_OF_DAY));
		arr[4]=String.valueOf(now.get(Calendar.MINUTE));
		arr[5]=String.valueOf(now.get(Calendar.SECOND));
		return arr;
	}

	public static String quartzTimeCrob(Date date, int minute) {
		StringBuilder builder = new StringBuilder();
		String[] arr=getTimeStrArr(new Date(),-10);
		////0 0 0 29 3 2014
		builder.append(arr[5]).append(" ").append(arr[4]).append(" ").append(arr[3]).append(" ").append(arr[2]).append(" ").append(arr[1]).append(" ").append(arr[0]);
		return builder.toString();
	}
	public static void main(String[] args) throws ParseException {
		System.out.println(getLongByTimeStr("2017-10-10 03:03:23.323"));
	}
}